//-----------------------------------------------------------------------------
//
// FifteenPuzzle
//
// Eugene Magdel and Joshua Jung
//
// 2011 for University of Illinois at Chicago CS340 w/ Professor Troy
//
//-----------------------------------------------------------------------------
#include "fifteenpuzzle.h"
#include "ui_fifteenpuzzle.h"
#include <QGraphicsPixmapItem>
#include <QMessageBox>
#include <sstream>
#include "grid.h"

using namespace std;

//-----------------------------------------------------------------------------
//
// Constructor
//
//-----------------------------------------------------------------------------
FifteenPuzzle::FifteenPuzzle(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::FifteenPuzzle),
    graphicsScene(NULL)
{
    ui->setupUi(this);

    initialize();
}

//-----------------------------------------------------------------------------
//
// Destructor
//
//-----------------------------------------------------------------------------
FifteenPuzzle::~FifteenPuzzle()
{
    delete ui;
}

//-----------------------------------------------------------------------------
//
// Methods
//
//-----------------------------------------------------------------------------
/**
 * Initialize the fifteen puzzle.
 */
void FifteenPuzzle::initialize()
{
    if (graphicsScene == NULL)
    {
        graphicsScene = new QGraphicsScene(0, 0, (48 + 2) * 4, (48 + 2) * 4);

        ui->graphicsView->setScene(graphicsScene);
    }
   grid = new Grid(4);

   grid -> initialize_tiles(graphicsScene);
   grid -> draw_tiles();
}

/**
 * Quit the application
 */
void FifteenPuzzle::quit()
{
    QApplication::exit(0);
}

//-----------------------------------------------------------------------------
//
// Actions
//
//-----------------------------------------------------------------------------
/**
 * Open the "About" box
 */
void FifteenPuzzle::aboutClick()
{
    QMessageBox::about(this, tr("Fifteen Puzzle"),
                      tr("Fifteen Puzzle\nCS340\nUniversity of Illinois at Chicago for CS340\nApril 2011\nAuthors: Eugene Magdel and Joshua Jung\n"));
}

/**
 * Open the "Help" box
 */
void FifteenPuzzle::helpClick()
{
    QMessageBox::about(this, tr("Fifteen Puzzle Help"),
                      tr("Fifteen Puzzle: move the tiles around until they produce the solution picture.\nTo see the solution, click Game->Reset Board in the menu.\nTo start a new game, click Game->Mix Board in the menu.\nTo exit, click Game->Quit.\n You can view About and Help information in the menu used to access this window."));
}

/**
 * Mix the board.
 */
void FifteenPuzzle::on_actionMix_Board_triggered()
{
    // Solve first to reset tiles...
    grid -> solve();

    // Then mix endlessly until we find a solvable mix...
    grid -> mix();

    // Now redraw the tiles...
    grid -> draw_tiles();
}

/**
 * Solve the board by positioning tiles in their original positions.
 */
void FifteenPuzzle::on_actionNew_triggered()
{
    grid -> solve();
    grid -> draw_tiles();
}

/**
 * Handle a mouse click and move a tile if possible.
 */
void FifteenPuzzle::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        std::cout << "clicked x: " << event -> x() << " y:" << event -> y() << endl;
        grid -> click(event -> x(), event -> y());
    }
}
